// 获取文章列表
$(function () {

    let index = null
    initArtCateList()
    // 封装函数获取列表
    function initArtCateList() {
        axios({
            url: '/my/article/cates'
        }).then(({
            data: res
        }) => {
            //成功回调
            console.log(res)
            if (res.status != 0) {
                return layer.msg(res.message)
            }
            $('tbody').empty()
            res.data.forEach(element => {
                if (element.Id < 0) {
                    return
                }
                $('tbody').append(`
                   <tr>
                        <td>${element.Id}</td>
                        <td>${element.name}</td>
                        <td>${element.alias}</td>
                        <td>
                            <button data-id="${element.Id}" class = "layui-btn layui-btn layui-btn-xs btn_edit"> 编辑 </button>
                            <button data-id="${element.Id}" class="layui-btn layui-btn layui-btn-xs  layui-btn-danger btn_del">删除</button>
                        </td>
                    </tr>
                `)
            });
        });
    }

    // 添加类别结构
    $('#addBtn').on('click', function () {
        index = layer.open({
            type: 1,
            title: '添加文章类别',
            area: ['500px', '250px'],
            content: `
                <form id="addForm" class="layui-form" action="">
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类别名</label>
                        <div class="layui-input-block">
                            <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="formDemo">确认添加</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            `
        });
    })

    // 添加类别
    // 动态新增事件委托
    $('body').on('submit', '#addForm', function (e) {
        e.preventDefault()
        axios({
            url: '/my/article/addcates',
            method: 'POST',
            data: $(this).serialize()
        }).then(({
            data: res
        }) => {
            //成功回调
            console.log(res)
            if (res.status != 0) {
                return layer.msg(res.message)
            }
            layer.close(index)
            layer.msg('恭喜您新增成功')
            setTimeout(function () {
                initArtCateList()

            }, 1500)
        });
    })
    let indexEdit = null
    // 动态修改表单
    $('tbody').on('click', '.btn_edit', function () {
        indexEdit = layer.open({
            type: 1,
            title: '修改文章类别',
            area: ['500px', '250px'],
            content: `
                <form id="editForm" class="layui-form" lay-filter="editForm" action="">
                    <input type="hidden" name="Id">
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="name" required lay-verify="required" placeholder="请输入分类名称" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">分类别名</label>
                        <div class="layui-input-block">
                            <input type="text" name="alias" required lay-verify="required" placeholder="请输入分类别名" autocomplete="off"
                                class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn btn_edit" lay-submit lay-filter="formDemo">确认修改</button>
                            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                        </div>
                    </div>
                </form>
            `
        });
        // 获取元素
        axios({
            url: '/my/article/cates/' + $(this).attr('data-Id')
        }).then(({
            data: res
        }) => {
            //成功回调
            console.log(res)
            layui.form.val('editForm', res.data)
        });
    })

    $('body').on('submit', '#editForm', function (e) {
        e.preventDefault()
        axios({
            url: '/my/article/updatecate',
            method: 'POST',
            data: $(this).serialize()
        }).then(({
            data: res
        }) => {
            //成功回调
            console.log(res)
            if (res.status != 0) {
                return layer.msg(res.message)
            }
            layer.close(indexEdit)
            layer.msg('恭喜您修改成功')
            setTimeout(function () {
                initArtCateList()
            }, 1500)
        });
    })
    $('tbody').on('click', '.btn_del', function () {
        console.log();
        let id = $(this).attr('data-Id')
        layer.confirm('是否删除?', {
            icon: 3,
            title: '提示'
        }, function (index) {
            //do something
            axios({
                url: '/my/article/deletecate/' + id
            }).then(({
                data: res
            }) => {

                //成功回调
                if (res.status != 0) {
                    return layer.msg(res.message)
                }
                layer.msg('恭喜您删除成功成功')
                initArtCateList()
            });

            layer.close(index);
        });

    })
})